package com.example.dormitoryscoresystem.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.common.BitMatrix;

import java.io.File;
import java.nio.file.Path;

public class QRCodeGenerator {

    // 生成二维码的方法
    public static void generateQRCode(String content, String outputPath) throws Exception {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, 300, 300);

        Path path = new File(outputPath).toPath();
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    }

    // 主方法：运行这个类就可以生成二维码
    public static void main(String[] args) throws Exception {
        // 微信小程序二维码内容格式: "weixin://dl/officialaccounts?scene=xxxx&path=pages/score/score"
        String scene = "12345";  // 自定义场景值，可以是任意值，用于区分二维码
        String path = "pages/score/score";  // 目标页面路径

        // 创建二维码内容
        String content = "weixin://dl/officialaccounts?scene=" + scene + "&path=" + path;

        // 输出二维码图片的位置（相对项目路径）
        String outputPath = "2score_page_qr.png";

        // 生成二维码
        generateQRCode(content, outputPath);

        System.out.println("二维码已生成: " + outputPath);
    }
}
